package com.zmn.plat.dubbo.impl.orderconfig.returnvisitcontent;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.cglib.beans.BeanCopier;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitContentDIO;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitContentDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.returnvisitcontent.ReturnVisitContentListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.manager.dictionary.ProductDict;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitContent;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitContentQuery;
import com.zmn.plat.services.interfaces.orderconfig.returnvisitcontent.ReturnVisitContentService;
import com.zmn.plat.utils.bean.BeanCopyUtils;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述:回访内容
 *
 * @author linfeng
 * @since 2019/10/24/17:40
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class ReturnVisitContentListRemoteServiceImpl implements ReturnVisitContentListRemoteService {
    private static final String TAG = "订单配置-回访内容 dubbo 接口";

    @Resource
    private ReturnVisitContentService returnVisitContentService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<ReturnVisitContentDRO> getById(Integer id) {
        log.info("getById 参数为：id={}", id);
        ReturnVisitContentDRO dro = new ReturnVisitContentDRO();
        ReturnVisitContent returnVisitContent = returnVisitContentService.findByKey(id);
        if (returnVisitContent == null) {
            log.error(String.format("[%s]参数:[%s]对应的回访内容对象不存在", TAG, id));
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }
        BeanCopyUtils.copyProperties(returnVisitContent, dro);
        dro.setPlatName(ProductDict.getPlatNameByPlates(dro.getPlat()));
        return ResponseDTO.success(dro);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ReturnVisitContentDRO>> listByQuery(ReturnVisitContentDIO returnVisitContentDIO) {
        log.info("listByQuery 参数为：returnVisitContentDIO={}", returnVisitContentDIO);
        ReturnVisitContentQuery returnVisitContentQuery = new ReturnVisitContentQuery();
        BeanCopyUtils.copyProperties(returnVisitContentDIO, returnVisitContentQuery);
        List<ReturnVisitContent> returnVisitContentList = Optional.ofNullable(returnVisitContentService.listByQuery(returnVisitContentQuery)).orElse(Collections.emptyList());
        BeanCopier copier = BeanCopier.create(ReturnVisitContent.class, ReturnVisitContentDRO.class, false);
        List<ReturnVisitContentDRO> documentaryContentDROList = returnVisitContentList.stream().map(returnVisitContent -> {
            ReturnVisitContentDRO dro = new ReturnVisitContentDRO();
            copier.copy(returnVisitContent, dro, null);
            dro.setPlatName(ProductDict.getPlatNameByPlates(dro.getPlat()));
            return dro;
        }).collect(Collectors.toList());

        return ResponseDTO.success(documentaryContentDROList);
    }
}
